<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="仓库名称" prop="warehouseName" label-width="80px">
        <el-input
          v-model="queryParams.warehouseName"
          placeholder="请输入仓库名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="批次" prop="batchNum">
        <el-input
          v-model="queryParams.batchNum"
          placeholder="请输入批次(原料-产品批次)"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="负责人" prop="principal">
        <el-input
          v-model="queryParams.principal"
          placeholder="请输入负责人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="负责人联系方式" prop="phone" label-width="120px">
        <el-input
          v-model="queryParams.phone"
          placeholder="请输入负责人联系方式"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="仓库占地面积" prop="area" label-width="120px">
        <el-input
          v-model="queryParams.area"
          placeholder="请输入仓库占地面积"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['ewem:warehouse:add']"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['ewem:warehouse:edit']"
        >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['ewem:warehouse:remove']"
        >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['ewem:warehouse:export']"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="warehouseList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="仓库id" align="center" prop="warehouseId"/>
      <el-table-column label="仓库名称" align="center" prop="warehouseName"/>
      <el-table-column label="批次(原料-产品批次)" align="center" prop="batchNum"/>
      <el-table-column label="仓库位置" align="center" prop="address"/>
      <el-table-column label="负责人信息" align="center" prop="principal"/>
      <el-table-column label="负责人联系方式" align="center" prop="phone"/>
      <el-table-column label="库存数量" align="center" prop="inventory">
        <template v-slot="scope">
          {{scope.row.inventory}}吨
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" prop="status">
        <template v-slot="scope">
          <el-tag v-if="scope.row.status==0">启用</el-tag>
          <el-tag v-if="scope.row.status==1">停用</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="仓库占地面积面积" align="center" prop="area"/>
      <el-table-column label="备注" align="center" prop="remark"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleStockOut(scope.row)">出库</el-button>
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
                     v-hasPermi="['ewem:warehouse:edit']">修改
          </el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
                     v-hasPermi="['ewem:warehouse:remove']">删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改仓库对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="仓库名称" prop="warehouseName">
          <el-input v-model="form.warehouseName" placeholder="请输入仓库名称"/>
        </el-form-item>
        <el-form-item label="批次" prop="batchNum">
          <el-input v-model="form.batchNum" placeholder="请输入批次(原料-产品批次)"/>
        </el-form-item>
        <el-form-item label="仓库位置" label-width="100px">
          <el-cascader
            v-model="value"
            :options="addrOptions"
            @change="handleChange"></el-cascader>
        </el-form-item>
        <el-form-item label="负责人信息" prop="principal">
          <el-input v-model="form.principal" placeholder="请输入负责人信息"/>
        </el-form-item>
        <el-form-item label="联系方式" prop="phone">
          <el-input v-model="form.phone" placeholder="请输入负责人联系方式"/>
        </el-form-item>
        <el-form-item label="仓库占地面积" prop="area">
          <el-input v-model="form.area" placeholder="请输入仓库占地面积"/>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 出库对话框 -->
    <el-dialog :title="titles" :visible.sync="opens" width="500px" append-to-body>
      <el-form ref="form" :model="forms" :rules="rules" label-width="100px">
        <el-form-item label="仓库名称" prop="warehouseName">
          <el-input v-model="forms.warehouseName" disabled placeholder="请输入仓库名称"/>
        </el-form-item>
        <el-form-item label="批次" prop="batchNum">
          <el-input v-model="forms.batchNum" disabled placeholder="请输入批次(原料-产品批次)"/>
        </el-form-item>

        <el-form-item label="仓库位置" prop="remark">
          <el-input v-model="forms.address" disabled/>
        </el-form-item>

        <el-form-item label="出库数量" prop="remark">
          <el-input v-model="forms.inventory" placeholder="请输入出库数量"/>
        </el-form-item>

        <el-form-item label="目的地" prop="remark">
          <el-select v-model="forms.destination" placeholder="请选择目的地">
            <el-option v-for="w in warehouseOptions" :label="w.warehouseName" :value="w.warehouseId" v-bind:key="w.warehouseId"></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="物流司机">
          <el-select v-model="forms.drivers" placeholder="请选择物流司机">
            <el-option v-for="d in driversOptions" :ref="d.userId" :label="d.nickName" :value="d.userId" v-bind:key="d.userId"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForms">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
import * as echarts from 'echarts';
import {listWarehouse, getWarehouse, delWarehouse, addWarehouse, updateWarehouse,stockOut,getDrivers,countWarehouse} from "@/api/ewem/warehouse";

export default {
  name: "Warehouse",
  data() {
    return {
      value: [],
      values:[],
      addrOptions: [],
      driversOptions:[],
      warehouseOptions:[],
      titles: '',
      opens: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 仓库表格数据
      warehouseList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        warehouseName: null,
        batchNum: null,
        address: null,
        principal: null,
        phone: null,
        status: null,
        area: null,
      },
      // 表单参数
      form: {},
      // 出库对话框
      forms:{},
      // 表单校验
      rules: {},
      // 报表对话框
      examples:false,
    };
  },
  created() {
    this.getList();
    this.handleExamples();
  },
  methods: {
    /** 出库 */
    handleStockOut(row) {
      this.forms.batchNum=row.batchNum;
      this.forms.warehouseId=row.warehouseId;
      this.forms.warehouseName=row.warehouseName;
      this.forms.address=row.address;
      getDrivers(row.warehouseId).then(response => {
        this.driversOptions=response.drivers.rows;
        this.warehouseOptions=response.warehouse;
        this.opens = true;
        this.titles='出库';
      })
    },
    /** 提交出库信息 */
    submitForms(){
      stockOut(this.forms).then(response => {
        this.$modal.msgSuccess("出库成功");
        this.opens = false;
        this.getList();
      })
    },
    /** 查询仓库列表 */
    getList() {
      this.loading = true;
      listWarehouse(this.queryParams).then(response => {
        this.warehouseList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        warehouseId: null,
        warehouseName: null,
        batchNum: null,
        address: null,
        principal: null,
        phone: null,
        status: null,
        area: null,
        delFlag: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null,
      };
      this.resetForm("form");
      this.value=[];
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.warehouseId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      getWarehouse().then(response => {
        this.open = true;
        this.title = "添加仓库";
        this.addrOptions = response.treeNodes;
      })
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const warehouseId = row.warehouseId || this.ids
      getWarehouse(warehouseId).then(response => {
        this.addrOptions = response.treeNodes;
        this.form = response.data;
        this.value.push(this.form.provinceId+'',this.form.cityId+'',this.form.districtId+'');
        this.open = true;
        this.title = "修改仓库";
      });
    },
    // 省市区级联下拉框选中数据
    handleChange(value) {
      this.form.provinceId = value[0];
      this.form.cityId = value[1];
      this.form.districtId = value[2];
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.warehouseId != null) {
            updateWarehouse(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addWarehouse(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const warehouseIds = row.warehouseId || this.ids;
      this.$modal.confirm('是否确认删除仓库编号为"' + warehouseIds + '"的数据项？').then(function () {
        return delWarehouse(warehouseIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/warehouse/export', {
        ...this.queryParams
      }, `warehouse_${new Date().getTime()}.xlsx`)
    },
  }
};
</script>
